package com.endoc.agingtools.adapter;

import android.content.Context;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;

import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.endoc.agingtools.ItemBean;
import com.endoc.agingtools.R;
import com.endoc.agingtools.view.PopupWindows;
import java.util.List;

import cn.com.heaton.blelibrary.ble.Ble;
import cn.com.heaton.blelibrary.ble.model.BleDevice;


public class KaweiRecycleViewAdapter extends RecyclerView.Adapter<KaweiRecycleViewAdapter.MyViewHolder>{
    private Context context;
    private List<ItemBean> itemBeanList;
    private View inflater;
    List<BleDevice> mSearchDevice;
    RecyclerView mRecyclerView;
    //构造方法，传入数据
    public KaweiRecycleViewAdapter(Context context, List<ItemBean> itemBeanList, List<BleDevice> searchDevice, RecyclerView recyclerView){
        this.context = context;
        mSearchDevice =searchDevice;
        mRecyclerView =recyclerView;
        this.itemBeanList =itemBeanList;//设置列表集合
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //创建ViewHolder，返回每一项的布局
        inflater = LayoutInflater.from(context).inflate(R.layout.kawei_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.tvKaweiId.setText("卡位"+itemBeanList.get(position).getKaiwei());
        List<BleDevice> connectedDevices = Ble.getInstance().getConnectedDevices();
        String deviceName = itemBeanList.get(position).getDeviceName();

        if(deviceName!=null&&!deviceName.isEmpty()){
            holder.tvBindName.setText(deviceName);
            boolean isConnect =false;
            for (int i=0;i<connectedDevices.size();i++){
                if(connectedDevices.get(i).getBleName().equals(deviceName)){
                    isConnect =true;
                        break;
                }
            }
            holder.tvDeviceStatus.setText(isConnect?"连接":"断开");
            holder.tvDeviceStatus.setTextColor(isConnect? Color.parseColor("#FF5F51"):Color.parseColor("#323232"));
        }else {
            holder.tvBindName.setText("");
        }

        holder.tvKaweiSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindows.getPopWnd(context,holder.itemView.getRootView().getRootView(),itemBeanList.get(position),mSearchDevice,KaweiRecycleViewAdapter.this);
            }
        });
    }



    @Override
    public int getItemCount() {
        //返回Item总条数
        return itemBeanList.size();
    }

    //内部类，绑定控件
    class MyViewHolder extends RecyclerView.ViewHolder{
         TextView tvKaweiId;
         TextView tvBindName;
         Button tvKaweiSet;
         TextView tvDeviceStatus;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvKaweiId = (TextView) itemView.findViewById(R.id.tv_kawei_id);
            tvBindName = (TextView) itemView.findViewById(R.id.tv_bind_name);
            tvKaweiSet = (Button) itemView.findViewById(R.id.tv_kawei_set);
            tvDeviceStatus = (TextView) itemView.findViewById(R.id.tv_device_status);
        }
    }
}

